/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.univalle.persistencia;


import co.edu.univalle.modelo.Clientes;
import co.edu.univalle.modelo.Empleados;
import co.edu.univalle.modelo.Pedidos;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author valentina
 */
@Stateless
public class PedidoDAO implements IPedidoDAO {

         @PersistenceContext
    private EntityManager entityManager;

    
    @Override
    public void registrar(Pedidos pedido) {
      try{
         entityManager.persist(pedido);
     }
     catch (RuntimeException e){
     throw e;
    }
  
        
    }

    @Override
    public void modificar(Pedidos pedido) {
        try{
         entityManager.merge(pedido);
     }
     catch (RuntimeException e){
     throw e;
    }

    }

    @Override
    public void eliminar(Pedidos pedido) {
        try{
         entityManager.remove(pedido);
     }
     catch (RuntimeException e){
     throw e;
    }

    }

    @Override
    public List<Pedidos> consultar() {
         try{
                String jpql = "SELECT c FROM Clientes c";
                Query query = entityManager.createQuery(jpql);
                return query.getResultList();
         
     }
     catch (RuntimeException e){
     throw e;
}

    }

    @Override
    public Pedidos consultarPorIdPedido(Integer idpedido) {
         try{
                    return entityManager.find(Pedidos.class, idpedido);
     }
                
     catch (RuntimeException e){
     throw e;
       
        }
    }

    @Override
    public List<Pedidos> consultarPorIdCliente(Integer idcliente) {
        try{
                    String jpql = "SELECT c FROM Clientes c where idcliente.idcliente="+idcliente;
                    Query query = entityManager.createQuery(jpql);
                    return query.getResultList();
         
     }
     catch (RuntimeException e){
     throw e;
}
    }
    

    @Override
    public List<Pedidos> consultarPorIdEmpleado(Integer idempleado) {
        try{
                    String jpql = "SELECT c FROM Clientes c where idempleado.idempleado="+idempleado;
                    Query query = entityManager.createQuery(jpql);
                    return query.getResultList();
         
     }
     catch (RuntimeException e){
     throw e;
}
    }
    

  
    

    
}
